﻿using mscook.core.domain;
using mscook.core.services;
using mscook.mvc.core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mscook.mvc.Areas.weblogin.Models;
using System.Linq.Expressions;
namespace mscook.mvc.Areas.weblogin.Controllers
{
    public class materialController : BaseController
    {
        private IMaterialService iMaterialService;
        private IImageSettingFactory iImageSettingFactory;
        private ILoginUserService iLoginUserService;
        private readonly string SectionName = "Material";
        public materialController(IMaterialService iMaterialService, IImageSettingFactory iImageSettingFactory, ILoginUserService iLoginUserService):base(iLoginUserService)
        {
            this.iLoginUserService = iLoginUserService;
            this.iMaterialService = iMaterialService;
            this.iImageSettingFactory = iImageSettingFactory;
        }

        public ActionResult Index(string nowPageNum, string per = "",string materialName="")
        {

            Expression<Func<Material, bool>> select_expression = o => (string.IsNullOrEmpty(materialName) || o.Name.Contains(materialName));

            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(nowPageNum, per);
            long total = 0;
            IList<Material> MaterialList = iMaterialService.SelectPageListByExpressions(pageHelper.Start, pageHelper.PerValue, out total, select_expression);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);

            InitNavBar();

            ViewData["materialName"] = materialName;
           
            ViewData["nowPageNum"] = nowPageNum;
            ViewData["per"] = per;
            return View(MaterialList);
        }

        [HttpGet]
        public ActionResult Add()
        {
            InitNavBar();
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(Material Material, string MaterialTypeId,bool IsTop=false)
        {
            if (ModelState.IsValid)
            {
                if (SaveUploadImage(Material))
                {
                    Material.EntityIndex = IsTop ? 1 : 0;
                    SaveResourceType(Material, MaterialTypeId);
                    iMaterialService.SaveOrUpdate(Material);
                    return RedirectToIndex();
                }
            }
            return View();
        }

        [HttpGet]
        public ActionResult Edit(string id)
        {
            Material Material = iMaterialService.Get(id);
            if (Material != null)
            {
                MaterialModel materialModel = new MaterialModel() {Material=Material };
                materialModel.ResourceTypes = GetResourceType(Material.Id);               
                InitNavBar();
                return View(materialModel);
            }
            return RedirectToIndex();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(string id, string MaterialTypeId,bool IsTop=false)
        {
            MaterialModel materialModel = new MaterialModel();
            materialModel.Material = iMaterialService.Get(id);
           // Material material = iMaterialService.Get(id);
            if (ModelState.IsValid)
            {

                if (SaveUploadImage(materialModel.Material))
                {
                    materialModel.Material.EntityIndex = IsTop ? 1 : 0;
                    SaveResourceType(materialModel.Material, MaterialTypeId);
                    //UpdateModel(Material);
                    UpdateModel(materialModel);
                    iMaterialService.SaveOrUpdate(materialModel.Material);
                    return RedirectToIndex();
                }
            }
            return View();
        }

       

        public ActionResult Delete(string id)
        {
            iMaterialService.Delete(id);
            return RedirectToIndex();
        }

        public bool SaveUploadImage(Material Material)
        {
            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files.Get(0);
                if (file.ContentLength != 0)
                {
                    Material.ImageName = iImageSettingFactory.CreateBatchSizedImage(SectionName, file.FileName, file.InputStream);
                }
                return true;
            }
            return false;
        }

       

    }
}
